#include <QDebug>
#include "QFile"
#include "Init.h"
#include <QMap>
#include <QQueue>
#include <QList>
#include <QPointF>
#include <QString>
#include <qcolor.h>
#include<QStringList>
#include<QLineEdit>
#include <cmath>
#include<QDir>
QMap<QString,node> Station; //画图使用
QMap<QString,QList<QString> > edge; // 跑最短路
QMap<QString,QSet<QString> > Station_Line; // 站点所属线路总
QMap<QString,QColor> LineColor;  //  线路颜色
QMap<QString,QMap<QString,int> >dp;//dist
QMap<QString,QMap<QString,QSet<QString> > >mp; //两站站点共同的线路

double minLongi=10000;
double minLati=10000;
double maxLongi=0;
double maxLati=0;

void infoinit(){
    Station.clear();
    edge.clear();
    Station_Line.clear();
    LineColor.clear();
    dp.clear();
    mp.clear();
    QFile file("lineQD.txt");
    file.open(QIODevice::ReadOnly);
    if(!file.isOpen()){
        qDebug()<<"error open";
        return ;
    }
    QTextStream in(&file);
    QString buffer;
    QString Name;
    QString Color;
    int Num;
    while(true){
        in>>buffer>>buffer;
        if(in.atEnd()) break;
        in>>buffer>>buffer; // line name;
        Name = buffer;

        in>>buffer>>buffer; // line color
        Color = buffer;

        in>>buffer>>buffer; // start
        in>>buffer;         // end

        in>>buffer>>buffer; // station num
        Num = buffer.toInt();
        /*
        extern QMap<QString,node> Station; //画图使用
        extern QMap<QString,QList<QString> > edge; // 跑最短路
        extern QMap<QString,QSet<QString> > Station_Line; // 站点所属线路总
        extern QMap<QString,QString> LineColor;  //  线路颜色
        */
        node stal[100];
        for(int i=0;i<Num;i++){  // 存储站点信息，存储站点所属线路
            in>>buffer>>stal[i].name>>stal[i].n>>stal[i].e;
            updateBound(stal[i].e,stal[i].n);
        //    qDebug()<<stal[i].name<<stal[i].n<<stal[i].e<<"\n";
        //    qDebug()<<maxLati<<" "<<maxLongi<<" "<<minLati<<" "<<minLongi<<"\n";
            Station[stal[i].name] = stal[i];
            Station_Line[stal[i].name].insert(Name);
        }


        node sta,laststa;
        for(int i=0;i<Num;i++){  //存储边
            if(i>=1) { edge[stal[i].name].append(stal[i-1].name); dp[stal[i].name][stal[i-1].name]=getDistance(stal[i].name,stal[i-1].name);
                mp[stal[i].name][stal[i-1].name].insert(Name);
            }
            if(i<Num-1) { edge[stal[i].name].append(stal[i+1].name); dp[stal[i].name][stal[i+1].name]=getDistance(stal[i].name,stal[i+1].name);
                mp[stal[i].name][stal[i+1].name].insert(Name);
            }
        }
        //
        LineColor[Name] = QColor(Color);   // 存储线路颜色

    }
    //addline
    infoaddline();
    //addstation
    infoaddstation();
    //addedge
    infoaddedge();
    for(auto i:Station.keys()) {
        Station[i].coord.setY((maxLati-Station[i].e)/(maxLati-minLati)*500+100);
        Station[i].coord.setX((Station[i].n-minLongi)/(maxLongi-minLongi)*1000+100);
    //    qDebug()<<Station[i].coord<<"\n";
    }
    return;
}
void infoaddline(){

    QFile linefie("addline.txt");
/*  addline.txt
    linename linecolor
*/
    linefie.open(QIODevice::ReadOnly);
    if(!linefie.isOpen()){
        qDebug()<<"error open"<<"addline.txt";
        return ;
    }
    QString Linename;
    QString Color;
    QTextStream linein(&linefie);
    linein.setCodec("UTF-8");
    while(true){
        linein>>Linename>>Color;
        if(linein.atEnd()) break;
        LineColor[Linename] = QColor(Color);
    }
    linefie.close();
}

void infoaddstation(){
    QFile linefie("addstation.txt");
/*
 *  addstation.txt
 *  stationname linename e n
*/
    linefie.open(QIODevice::ReadOnly);
    if(!linefie.isOpen()){
        qDebug()<<"error open";
        return ;
    }
    QString stationname;
//    QString linename;
    QTextStream linein(&linefie);
    linein.setCodec("UTF-8");
    node stal;
    while(true){
       linein>>stal.name>>stal.e>>stal.n;
        if(linein.atEnd()) break;
        updateBound(stal.e,stal.n);
     //   qDebug()<<stal.name<<stal.n<<stal.e<<"\n";
     //   qDebug()<<maxLati<<" "<<maxLongi<<" "<<minLati<<" "<<minLongi<<"\n";
        stal.coord.setY((maxLati-stal.e)/(maxLati-minLati)*500+100);
        stal.coord.setX((stal.n-minLongi)/(maxLongi-minLongi)*1000+100);
        Station[stal.name] = stal;
//        Station_Line[stal.name].insert(linename);
    }
    linefie.close();
}

void infoaddedge(){
//    QFile linefie("../testgitee/test/addedge.txt");

//    /*
// *  addedge.txt
// *  sta1 sta2 linename
//*/
//    linefie.open(QIODevice::ReadOnly);
//    if(!linefie.isOpen()){
//        qDebug()<<"error open";
//        return ;
//    }
//    QString sta1name,sta2name;
//    QString Linename;
//    QTextStream linein(&linefie);
//    linein.setCodec("UTF-8");
//    while(true){
//        linein>>sta1name>>sta2name>>Linename;
//        if(linein.atEnd()) break;
//        edge[sta1name].append(sta2name);
//        edge[sta2name].append(sta1name);
//        dp[sta1name][sta2name] = dp[sta2name][sta1name] = getDistance(sta1name,sta2name);
//        mp[sta1name][sta2name].insert(Linename);
//        mp[sta2name][sta1name].insert(Linename);
//    }
//    linefie.close();
}

//Latitude and longitude）即经线和纬线
void updateBound(const double lati,const double longi){
    if(minLati>lati)
        minLati=lati;
    if(maxLati<lati)
        maxLati=lati;
    if(minLongi>longi)
        minLongi=longi;
    if(maxLongi<longi)
        maxLongi=longi;
}

int getDistance(const QString sta1,const QString sta2){
    const double EARTH_RADIUS = 6378.137;

    double rlati1 = Station[sta1].n * M_PI / 180;
    double rlati2 = Station[sta2].n * M_PI / 180;
    double rlongi1 = Station[sta1].e * M_PI / 180;
    double rlongi2 = Station[sta2].e * M_PI / 180;

    double vlongi = abs(rlongi1-rlongi2);
    double vlati = abs(rlati1-rlati2);

    double h = HaverSin(vlati)+cos(rlati1)*cos(rlati2)*HaverSin(vlongi);
    double ret = 2*EARTH_RADIUS*asin(sqrt(h));

    return (int)(ret*1000);
}

double HaverSin(double theta)
{
    double v = sin(theta / 2);
    return v * v;
}

void initcombox(QComboBox *combox1,QComboBox *combox2){
    combox1->clear();combox2->clear();
    QStringList sta_name_list;
    for(auto &i:Station.keys())
        sta_name_list.append(i);
    std::sort(sta_name_list.begin(),sta_name_list.end(),[](const QString &s1, const QString &s2){
        return (s1.localeAwareCompare(s2) < 0);
    });
    combox1->addItems(sta_name_list);
    combox2->addItems(sta_name_list);
    QLineEdit *line1 = new QLineEdit;
    combox1->setLineEdit(line1);
    combox1->lineEdit()->clear();
    QLineEdit *line2 = new QLineEdit;
    combox2->setLineEdit(line2);
    combox2->lineEdit()->clear();
}

void ddebug(){
    qDebug()<<"all station\n";
    QString s;
    for(auto &i:Station.keys()) {
        qDebug()<<Station[i].name<<" "<<Station[i].e<<" "<<Station[i].n<<" ";
        s=*Station_Line[i].begin();
        if(Station_Line[i].size()!=1)  qDebug()<<"black  ";
    }
}
